__author__ = 'ipetrov'

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.expected_conditions import *

import unittest

class any_window_other_than(object):
    def __init__(self, existing_windows):
        self.existing_windows = existing_windows

    def __call__(self, driver):
        handles = driver.window_handles
        diff = set(handles)-set(self.existing_windows)
        return iter(diff).next() if len(diff) > 0 else False

class Untitled(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "http://localhost/"
        self.verificationErrors = []
        self.accept_next_alert = True

    def open_and_login(self):
        driver = self.driver
        try:
            driver.get(self.base_url + "/php4dvd/")
            driver.find_element_by_id("username").clear()
            driver.find_element_by_id("username").send_keys("admin")
            driver.find_element_by_name("password").clear()
            driver.find_element_by_name("password").send_keys("admin")
            driver.find_element_by_name("submit").click()
            WebDriverWait(driver, 30).until(presence_of_element_located((By.ID, "q")))
        except:
            raise Exception("Login failed.")

    def remove_movie(self):
        driver = self.driver
        current_count = len(driver.find_element_by_id("results").find_elements_by_xpath("./a"))
        driver.find_element_by_css_selector("div.nocover").click()
        WebDriverWait(driver, 30).until(lambda driver: driver.find_element_by_css_selector("img[alt=\"Remove\"]"))
        driver.find_element_by_css_selector("img[alt=\"Remove\"]").click()
        WebDriverWait(driver, 10).until(alert_is_present())
        driver.switch_to_alert().accept()
        WebDriverWait(driver, 30).until(presence_of_element_located((By.ID, "q")))
        if len(driver.find_element_by_id("results").find_elements_by_xpath("./a")) >= current_count:
            raise Exception("Movie didn't delete")

    def test_untitled(self):
        self.open_and_login()
        self.remove_movie()

    def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException, e: return False
        return True

    def is_alert_present(self):
        try: self.driver.switch_to_alert()
        except NoAlertPresentException, e: return False
        return True

    def close_alert_and_get_its_text(self):
        try:
            alert = self.driver.switch_to_alert()
            alert_text = alert.text
            if self.accept_next_alert:
                alert.accept()
            else:
                alert.dismiss()
            return alert_text
        finally: self.accept_next_alert = True

    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()